﻿using LogSubstPol;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Text;

namespace TestProject1
{
    
    
    /// <summary>
    ///This is a test class for LogSubstPolTest and is intended
    ///to contain all LogSubstPolTest Unit Tests
    ///</summary>
  [TestClass()]
  public class LogSubstPolTest
  {
    #region Context

    private TestContext testContextInstance;

    /// <summary>
    ///Gets or sets the test context which provides
    ///information about and functionality for the current test run.
    ///</summary>
    public TestContext TestContext
    {
      get
      {
        return testContextInstance;
      }
      set
      {
        testContextInstance = value;
      }
    }

    #endregion

    #region Additional test attributes
    // 
    //You can use the following additional attributes as you write your tests:
    //
    //Use ClassInitialize to run code before running the first test in the class
    //[ClassInitialize()]
    //public static void MyClassInitialize(TestContext testContext)
    //{
    //}
    //
    //Use ClassCleanup to run code after all tests in a class have run
    //[ClassCleanup()]
    //public static void MyClassCleanup()
    //{
    //}
    //
    //Use TestInitialize to run code before running each test
    //[TestInitialize()]
    //public void MyTestInitialize()
    //{
    //}
    //
    //Use TestCleanup to run code after each test has run
    //[TestCleanup()]
    //public void MyTestCleanup()
    //{
    //}
    //
    #endregion


    #region ReplaceKeywordTest
    /// <summary>
    ///A test for ReplaceKeyword
    ///</summary>
    [TestMethod()]
    [DeploymentItem("LogSubstPol.dll")]
    public void ReplaceKeywordTest_01()
    {
      LogSubstPol_Accessor target = new LogSubstPol_Accessor(); // TODO: Initialize to an appropriate value
      Encoding enc = System.Text.Encoding.Default;
      byte[] bufferText = enc.GetBytes("1234567890$author$  \n  m e");
      int len = bufferText.Length;
      int maxLen = len + 1024;
      byte[] buffer = new byte[maxLen];
      bufferText.CopyTo(buffer, 0);
      bool expected = true;
      string text2Insert = "THIS IS THE NAME OF THE AUTHOR";
      int changeStartPos;
      bool actual = target.ReplaceKeyword(buffer, ref len, maxLen, "author", text2Insert, enc, out changeStartPos);
      Assert.AreEqual(expected, actual);

      byte[] bufferText2 = enc.GetBytes("1234567890$author: " + text2Insert + " $  \n  m e");
      Assert.AreEqual(len, bufferText2.Length);
      for(int i=0; i<len; i++)
         Assert.AreEqual(buffer[i], bufferText2[i]);
    }

    /// <summary>
    ///A test for ReplaceKeyword
    ///</summary>
    [TestMethod()]
    [DeploymentItem("LogSubstPol.dll")]
    public void ReplaceKeywordTest_02()
    {
      LogSubstPol_Accessor target = new LogSubstPol_Accessor(); // TODO: Initialize to an appropriate value
      Encoding enc = System.Text.Encoding.Default;
      byte[] bufferText = enc.GetBytes("1234567890$author: TEST$  \n  m e");
      int len = bufferText.Length;
      int maxLen = len + 1024;
      byte[] buffer = new byte[maxLen];
      bufferText.CopyTo(buffer, 0);
      bool expected = true;
      string text2Insert = "THIS IS THE NAME OF THE AUTHOR";
      int changeStartPos;
      bool actual = target.ReplaceKeyword(buffer, ref len, maxLen, "author", text2Insert, enc, out changeStartPos);
      Assert.AreEqual(expected, actual);

      byte[] bufferText2 = enc.GetBytes("1234567890$author: " + text2Insert + " $  \n  m e");
      Assert.AreEqual(len, bufferText2.Length, string.Format("Expected: {0}, Actual: {1}", enc.GetString(bufferText2), enc.GetString(buffer, 0, len)));
      for (int i = 0; i < len; i++)
        Assert.AreEqual(buffer[i], bufferText2[i], string.Format("Expected: {0}, Actual: {1}", enc.GetString(bufferText2), enc.GetString(buffer, 0, len)));
    }

    /// <summary>
    ///A test for ReplaceKeyword
    ///</summary>
    [TestMethod()]
    [DeploymentItem("LogSubstPol.dll")]
    public void ReplaceKeywordTest_03()
    {
      LogSubstPol_Accessor target = new LogSubstPol_Accessor(); // TODO: Initialize to an appropriate value
      Encoding enc = System.Text.Encoding.Default;
      byte[] bufferText = enc.GetBytes("1234567890$author: EIN GANZ LANGER TEXT, DER VORHER DRIN WAR$  \n  m e");
      int len = bufferText.Length;
      int maxLen = len + 1024;
      byte[] buffer = new byte[maxLen];
      bufferText.CopyTo(buffer, 0);
      bool expected = true;
      string text2Insert = "THIS IS THE NAME OF THE AUTHOR";
      int changeStartPos;
      bool actual = target.ReplaceKeyword(buffer, ref len, maxLen, "author", text2Insert, enc, out changeStartPos);
      Assert.AreEqual(expected, actual);

      byte[] bufferText2 = enc.GetBytes("1234567890$author: " + text2Insert + " $  \n  m e");
      Assert.AreEqual(len, bufferText2.Length, string.Format("Expected: {0}, Actual: {1}", enc.GetString(bufferText2), enc.GetString(buffer, 0, len)));
      for (int i = 0; i < len; i++)
        Assert.AreEqual(buffer[i], bufferText2[i], string.Format("Expected: {0}, Actual: {1}", enc.GetString(bufferText2), enc.GetString(buffer, 0, len)));
    }
    #endregion

    ///// <summary>
    /////A test for InsertPossibleComment
    /////</summary>
    //[TestMethod()]
    //[DeploymentItem("LogSubstPol.dll")]
    //public void InsertPossibleCommentTest()
    //{
    //  LogSubstPol_Accessor target = new LogSubstPol_Accessor(); // TODO: Initialize to an appropriate value
    //  string c = string.Empty; // TODO: Initialize to an appropriate value
    //  string cExpected = string.Empty; // TODO: Initialize to an appropriate value
    //  string[] clines = null; // TODO: Initialize to an appropriate value
    //  int commentIdx = 0; // TODO: Initialize to an appropriate value
    //  int commentIdxExpected = 0; // TODO: Initialize to an appropriate value
    //  target.InsertPossibleComment(ref c, clines, ref commentIdx);
    //  Assert.AreEqual(cExpected, c);
    //  Assert.AreEqual(commentIdxExpected, commentIdx);
    //  Assert.Inconclusive("A method that does not return a value cannot be verified.");
    //}

    #region IndexOfText
    /// <summary>
    ///A test for IndexOf
    ///</summary>
    [TestMethod()]
    [DeploymentItem("LogSubstPol.dll")]
    public void IndexOfTest()
    {
      LogSubstPol_Accessor target = new LogSubstPol_Accessor(); // TODO: Initialize to an appropriate value
      Encoding enc = System.Text.Encoding.Default;
      byte[] buffer = enc.GetBytes("12345678901$log$ with $log$");
      int startIdx = 2;
      int len = buffer.Length-startIdx;
      byte[] pattern = enc.GetBytes("$log$");
      int expected = 11;
      int actual;
      actual = target.IndexOf(buffer, startIdx, len, pattern);
      Assert.AreEqual(expected, actual);
    }
    #endregion

    ///// <summary>
    /////A test for GetNewLineChars
    /////</summary>
    //[TestMethod()]
    //[DeploymentItem("LogSubstPol.dll")]
    //public void GetNewLineCharsTest()
    //{
    //  LogSubstPol_Accessor target = new LogSubstPol_Accessor(); // TODO: Initialize to an appropriate value
    //  byte[] buffer = null; // TODO: Initialize to an appropriate value
    //  int len = 0; // TODO: Initialize to an appropriate value
    //  Encoding enc = null; // TODO: Initialize to an appropriate value
    //  byte[] expected = null; // TODO: Initialize to an appropriate value
    //  byte[] actual;
    //  actual = target.GetNewLineChars(buffer, len, enc);
    //  Assert.AreEqual(expected, actual);
    //  Assert.Inconclusive("Verify the correctness of this test method.");
    //}

    #region FindNextCRLF - \n
    /// <summary>
    ///A test for FindNextCRLF
    ///</summary>
    [TestMethod()]
    [DeploymentItem("LogSubstPol.dll")]
    public void FindNextCRLFTest_n_01()
    {
      LogSubstPol_Accessor target = new LogSubstPol_Accessor(); // TODO: Initialize to an appropriate value
      Encoding enc = System.Text.Encoding.Default;
      byte[] buffer = enc.GetBytes("1234567890\nwith one CR");
      int startIdx = 0;
      int len = buffer.Length;
      int expected = 10;
      int actual;
      actual = target.FindNextCRLF(buffer, startIdx, len, enc);
      Assert.AreEqual(expected, actual);
    }

    /// <summary>
    ///A test for FindNextCRLF
    ///</summary>
    [TestMethod()]
    [DeploymentItem("LogSubstPol.dll")]
    public void FindNextCRLFTest_n_02()
    {
      LogSubstPol_Accessor target = new LogSubstPol_Accessor(); // TODO: Initialize to an appropriate value
      Encoding enc = System.Text.Encoding.Default;
      byte[] buffer = enc.GetBytes("\nwith one CR");
      int startIdx = 0;
      int len = buffer.Length;
      int expected = 0;
      int actual;
      actual = target.FindNextCRLF(buffer, startIdx, len, enc);
      Assert.AreEqual(expected, actual);
    }

    /// <summary>
    ///A test for FindNextCRLF
    ///</summary>
    [TestMethod()]
    [DeploymentItem("LogSubstPol.dll")]
    public void FindNextCRLFTest_n_03()
    {
      LogSubstPol_Accessor target = new LogSubstPol_Accessor(); // TODO: Initialize to an appropriate value
      Encoding enc = System.Text.Encoding.Default;
      byte[] buffer = enc.GetBytes("12345\n");
      int startIdx = 0;
      int len = buffer.Length;
      int expected = 5;
      int actual;
      actual = target.FindNextCRLF(buffer, startIdx, len, enc);
      Assert.AreEqual(expected, actual);
    }

    /// <summary>
    ///A test for FindNextCRLF
    ///</summary>
    [TestMethod()]
    [DeploymentItem("LogSubstPol.dll")]
    public void FindNextCRLFTest_n_04()
    {
      LogSubstPol_Accessor target = new LogSubstPol_Accessor(); // TODO: Initialize to an appropriate value
      Encoding enc = System.Text.Encoding.Default;
      byte[] buffer = enc.GetBytes("\n");
      int startIdx = 0;
      int len = buffer.Length;
      int expected = 0;
      int actual;
      actual = target.FindNextCRLF(buffer, startIdx, len, enc);
      Assert.AreEqual(expected, actual);
    }
    #endregion

    #region FindNextCRLF - \r
    /// <summary>
    ///A test for FindNextCRLF
    ///</summary>
    [TestMethod()]
    [DeploymentItem("LogSubstPol.dll")]
    public void FindNextCRLFTest_r_01()
    {
      LogSubstPol_Accessor target = new LogSubstPol_Accessor(); // TODO: Initialize to an appropriate value
      Encoding enc = System.Text.Encoding.Default;
      byte[] buffer = enc.GetBytes("1234567890\rwith one CR");
      int startIdx = 0;
      int len = buffer.Length;
      int expected = 10;
      int actual;
      actual = target.FindNextCRLF(buffer, startIdx, len, enc);
      Assert.AreEqual(expected, actual);
    }

    /// <summary>
    ///A test for FindNextCRLF
    ///</summary>
    [TestMethod()]
    [DeploymentItem("LogSubstPol.dll")]
    public void FindNextCRLFTest_r_02()
    {
      LogSubstPol_Accessor target = new LogSubstPol_Accessor(); // TODO: Initialize to an appropriate value
      Encoding enc = System.Text.Encoding.Default;
      byte[] buffer = enc.GetBytes("\rwith one CR");
      int startIdx = 0;
      int len = buffer.Length;
      int expected = 0;
      int actual;
      actual = target.FindNextCRLF(buffer, startIdx, len, enc);
      Assert.AreEqual(expected, actual);
    }

    /// <summary>
    ///A test for FindNextCRLF
    ///</summary>
    [TestMethod()]
    [DeploymentItem("LogSubstPol.dll")]
    public void FindNextCRLFTest_r_03()
    {
      LogSubstPol_Accessor target = new LogSubstPol_Accessor(); // TODO: Initialize to an appropriate value
      Encoding enc = System.Text.Encoding.Default;
      byte[] buffer = enc.GetBytes("12345\r");
      int startIdx = 0;
      int len = buffer.Length;
      int expected = 5;
      int actual;
      actual = target.FindNextCRLF(buffer, startIdx, len, enc);
      Assert.AreEqual(expected, actual);
    }

    /// <summary>
    ///A test for FindNextCRLF
    ///</summary>
    [TestMethod()]
    [DeploymentItem("LogSubstPol.dll")]
    public void FindNextCRLFTest_r_04()
    {
      LogSubstPol_Accessor target = new LogSubstPol_Accessor(); // TODO: Initialize to an appropriate value
      Encoding enc = System.Text.Encoding.Default;
      byte[] buffer = enc.GetBytes("\r");
      int startIdx = 0;
      int len = buffer.Length;
      int expected = 0;
      int actual;
      actual = target.FindNextCRLF(buffer, startIdx, len, enc);
      Assert.AreEqual(expected, actual);
    }
    #endregion

    #region FindNextCRLF -
    /// <summary>
    ///A test for FindNextCRLF
    ///</summary>
    [TestMethod()]
    [DeploymentItem("LogSubstPol.dll")]
    public void FindNextCRLFTest_01()
    {
      LogSubstPol_Accessor target = new LogSubstPol_Accessor(); // TODO: Initialize to an appropriate value
      Encoding enc = System.Text.Encoding.Default;
      byte[] buffer = enc.GetBytes("none CR");
      int startIdx = 0;
      int len = buffer.Length;
      int expected = -1;
      int actual;
      actual = target.FindNextCRLF(buffer, startIdx, len, enc);
      Assert.AreEqual(expected, actual);
    }

    /// <summary>
    ///A test for FindNextCRLF
    ///</summary>
    [TestMethod()]
    [DeploymentItem("LogSubstPol.dll")]
    public void FindNextCRLFTest_02()
    {
      LogSubstPol_Accessor target = new LogSubstPol_Accessor(); // TODO: Initialize to an appropriate value
      Encoding enc = System.Text.Encoding.Default;
      byte[] buffer = enc.GetBytes("1234567890\r\n1234567890\r\n");
      int startIdx = 0;
      int len = buffer.Length;
      int expected = 10;
      int actual;
      actual = target.FindNextCRLF(buffer, startIdx, len, enc);
      Assert.AreEqual(expected, actual);
    }

    /// <summary>
    ///A test for FindNextCRLF
    ///</summary>
    [TestMethod()]
    [DeploymentItem("LogSubstPol.dll")]
    public void FindNextCRLFTest_03()
    {
      LogSubstPol_Accessor target = new LogSubstPol_Accessor(); // TODO: Initialize to an appropriate value
      Encoding enc = System.Text.Encoding.Default;
      byte[] buffer = enc.GetBytes("1234567890\n\r1234567890\n\r");
      int startIdx = 0;
      int len = buffer.Length;
      int expected = 10;
      int actual;
      actual = target.FindNextCRLF(buffer, startIdx, len, enc);
      Assert.AreEqual(expected, actual);
    }
    #endregion

    #region FindLogIdx
    /// <summary>
    ///A test for FindLogIdx
    ///</summary>
    [TestMethod()]
    [DeploymentItem("LogSubstPol.dll")]
    public void FindLogIdxTest_1()
    {
      LogSubstPol_Accessor target = new LogSubstPol_Accessor(); // TODO: Initialize to an appropriate value
      target._InsertBeforeLogLine = true;
      Encoding enc = System.Text.Encoding.Default;
      string prefixString = "// Comment: ";
      byte[] buffer = enc.GetBytes(prefixString + "$log$\n  m e");
      int len = buffer.Length;
      byte[] prefix = null;
      byte[] prefixExpected = enc.GetBytes(prefixString);
      int expected = 0;  // Insert *before* this line (so starting at 0)
      int actual;
      actual = target.FindLogIdx(buffer, len, out prefix, enc);
      Assert.AreEqual(prefixExpected.Length, prefix.Length, string.Format("Expected: {0}, Actual: {1}", enc.GetString(prefixExpected), enc.GetString(prefix)));
      for (int i = 0; i < prefix.Length; i++)
        Assert.AreEqual(prefixExpected[i], prefix[i], string.Format("Expected: {0}, Actual: {1}", enc.GetString(prefixExpected), enc.GetString(prefix)));
      Assert.AreEqual(expected, actual);
    }

    /// <summary>
    ///A test for FindLogIdx
    ///<remarks>
    ///Test for Last-Line with "$log$"
    ///</remarks>
    ///</summary>
    [TestMethod()]
    [DeploymentItem("LogSubstPol.dll")]
    public void FindLogIdxTest_2()
    {
      LogSubstPol_Accessor target = new LogSubstPol_Accessor(); // TODO: Initialize to an appropriate value
      target._InsertBeforeLogLine = true;
      Encoding enc = System.Text.Encoding.Default;
      string prefixString = "// Comment: ";
      byte[] buffer = enc.GetBytes(prefixString + "$log$");
      int len = buffer.Length;
      byte[] prefix = null;
      byte[] prefixExpected = enc.GetBytes(prefixString);
      int expected = 0;  // Insert *before* this line (so starting at 0)
      int actual;
      actual = target.FindLogIdx(buffer, len, out prefix, enc);
      Assert.AreEqual(prefixExpected.Length, prefix.Length, string.Format("Expected: {0}, Actual: {1}", enc.GetString(prefixExpected), enc.GetString(prefix)));
      for (int i = 0; i < prefix.Length; i++)
        Assert.AreEqual(prefixExpected[i], prefix[i], string.Format("Expected: {0}, Actual: {1}", enc.GetString(prefixExpected), enc.GetString(prefix)));
      Assert.AreEqual(expected, actual);
    }

    /// <summary>
    ///A test for FindLogIdx
    ///</summary>
    [TestMethod()]
    [DeploymentItem("LogSubstPol.dll")]
    public void FindLogIdxTest_3()
    {
      LogSubstPol_Accessor target = new LogSubstPol_Accessor(); // TODO: Initialize to an appropriate value
      target._InsertBeforeLogLine = true;
      Encoding enc = System.Text.Encoding.Default;
      string prefixString = "// Comment: ";
      byte[] buffer = enc.GetBytes(prefixString + "$Log$\n  m e");
      int len = buffer.Length;
      byte[] prefix = null;
      byte[] prefixExpected = enc.GetBytes(prefixString);
      int expected = 0;  // Insert *before* this line (so starting at 0)
      int actual;
      actual = target.FindLogIdx(buffer, len, out prefix, enc);
      Assert.AreEqual(prefixExpected.Length, prefix.Length, string.Format("Expected: {0}, Actual: {1}", enc.GetString(prefixExpected), enc.GetString(prefix)));
      for (int i = 0; i < prefix.Length; i++)
        Assert.AreEqual(prefixExpected[i], prefix[i], string.Format("Expected: {0}, Actual: {1}", enc.GetString(prefixExpected), enc.GetString(prefix)));
      Assert.AreEqual(expected, actual);
    }

    /// <summary>
    ///A test for FindLogIdx
    ///</summary>
    [TestMethod()]
    [DeploymentItem("LogSubstPol.dll")]
    public void FindLogIdxTest_4()
    {
      LogSubstPol_Accessor target = new LogSubstPol_Accessor(); // TODO: Initialize to an appropriate value
      target._InsertBeforeLogLine = true;
      Encoding enc = System.Text.Encoding.Default;
      string prefixString = "// Comment: ";
      byte[] buffer = enc.GetBytes(prefixString + "$LOG$\n  m e");
      int len = buffer.Length;
      byte[] prefix = null;
      byte[] prefixExpected = enc.GetBytes(prefixString);
      int expected = 0;  // Insert *before* this line (so starting at 0)
      int actual;
      actual = target.FindLogIdx(buffer, len, out prefix, enc);
      Assert.AreEqual(prefixExpected.Length, prefix.Length, string.Format("Expected: {0}, Actual: {1}", enc.GetString(prefixExpected), enc.GetString(prefix)));
      for (int i = 0; i < prefix.Length; i++)
        Assert.AreEqual(prefixExpected[i], prefix[i], string.Format("Expected: {0}, Actual: {1}", enc.GetString(prefixExpected), enc.GetString(prefix)));
      Assert.AreEqual(expected, actual);
    }

    /// <summary>
    ///A test for FindLogIdx
    ///<remarks>
    ///Several $log$ entries, but only one with \n at the end...
    ///</remarks>
    ///</summary>
    [TestMethod()]
    [DeploymentItem("LogSubstPol.dll")]
    public void FindLogIdxTest_5()
    {
      LogSubstPol_Accessor target = new LogSubstPol_Accessor(); // TODO: Initialize to an appropriate value
      target._InsertBeforeLogLine = true;
      Encoding enc = System.Text.Encoding.Default;
      string prePrefixString = "asdasdas dasfsd$Log$asdasdas\nsadasdds $LOG$     \n";
      string prefixString = "// Comment: ";
      byte[] buffer = enc.GetBytes(prePrefixString + prefixString + "$log$\nsadadasdasdfdsa");
      int len = buffer.Length;
      byte[] prefix = null;
      byte[] prefixExpected = enc.GetBytes(prefixString);
      int expected = enc.GetBytes(prePrefixString).Length;  // Insert *before* this line (so starting at 0)
      int actual;
      actual = target.FindLogIdx(buffer, len, out prefix, enc);
      Assert.AreEqual(prefixExpected.Length, prefix.Length, string.Format("Expected: {0}, Actual: {1}", enc.GetString(prefixExpected), enc.GetString(prefix)));
      for (int i = 0; i < prefix.Length; i++)
        Assert.AreEqual(prefixExpected[i], prefix[i], string.Format("Expected: {0}, Actual: {1}", enc.GetString(prefixExpected), enc.GetString(prefix)));
      Assert.AreEqual(expected, actual);
    }

    /// <summary>
    ///A test for FindLogIdx
    ///<remarks>
    ///Several $log$ entries, but only one with \n at the end...
    ///</remarks>
    ///</summary>
    [TestMethod()]
    [DeploymentItem("LogSubstPol.dll")]
    public void FindLogIdxTest_6()
    {
      LogSubstPol_Accessor target = new LogSubstPol_Accessor(); // TODO: Initialize to an appropriate value
      target._InsertBeforeLogLine = false;
      Encoding enc = System.Text.Encoding.Default;
      string prefixString = "// Comment: ";
      byte[] buffer = enc.GetBytes(prefixString + "$log$\n  m e");
      int len = buffer.Length;
      byte[] prefix = null;
      byte[] prefixExpected = enc.GetBytes(prefixString);
      int expected = enc.GetBytes(prefixString + "$log$\n").Length;  // Insert *after* this line
      int actual;
      actual = target.FindLogIdx(buffer, len, out prefix, enc);
      Assert.AreEqual(prefixExpected.Length, prefix.Length, string.Format("Expected: {0}, Actual: {1}", enc.GetString(prefixExpected), enc.GetString(prefix)));
      for (int i = 0; i < prefix.Length; i++)
        Assert.AreEqual(prefixExpected[i], prefix[i], string.Format("Expected: {0}, Actual: {1}", enc.GetString(prefixExpected), enc.GetString(prefix)));
      Assert.AreEqual(expected, actual);
    }
    #endregion

    #region DetectEncoding
    /// <summary>
    ///A test for DetectEncoding
    ///</summary>
    [TestMethod()]
    [DeploymentItem("LogSubstPol.dll")]
    public void DetectEncodingTest()
    {
      LogSubstPol_Accessor target = new LogSubstPol_Accessor(); // TODO: Initialize to an appropriate value
      Encoding expected = System.Text.Encoding.Default;
      byte[] buffer = expected.GetBytes("This is an test without BOM");
      int len = buffer.Length; // TODO: Initialize to an appropriate value
      Encoding actual;
      actual = target.DetectEncoding(buffer, len);
      Assert.AreEqual(expected, actual);
    }
    #endregion

  }
}
